package com.fenghuajueli.libbasecoreui.utils;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;

/**
 * 自定义水平间隔线
 */
public class CustomItemDecoration extends RecyclerView.ItemDecoration {
    private float horizontalWidth;
    private float firstLeftMargin;
    private float topMargin = -1;

    public CustomItemDecoration(Context context, float width) {
        horizontalWidth = width;
        firstLeftMargin = ConvertUtils.dp2px(17);
    }

    public CustomItemDecoration(Context context, float width, float firstLeftMargin) {
        horizontalWidth = width;
        this.firstLeftMargin = firstLeftMargin;
    }

    public CustomItemDecoration(Context context, float width, float firstLeftMargin, float topMargin) {
        horizontalWidth = width;
        this.firstLeftMargin = firstLeftMargin;
        this.topMargin = topMargin;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        if (position == 0) {
            outRect.left = (int) firstLeftMargin;
        } else {
            outRect.left = (int) horizontalWidth;
        }
        if (topMargin>=0) {
            outRect.top = (int) topMargin;
        }
        if (position == parent.getAdapter().getItemCount() - 1) {
            outRect.right = (int) firstLeftMargin;
        } else {
            outRect.right = 0;
        }


    }
}
